<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期筛选" label-width="100">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[
            new Date(2000, 1, 1, 0, 0, 0),
            new Date(2000, 1, 1, 23, 59, 59),
          ]"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="增减明细" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="增减明细"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="增减类型" prop="pm">
        <el-select @change="handleQuery" v-model="queryParams.pm" placeholder="请选择">
          <el-option label="增加" value="1"></el-option>
          <el-option label="消耗" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:userIntegralLog:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:userIntegralLog:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:userIntegralLog:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:userIntegralLog:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userIntegralLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="时间" align="center" prop="createTime" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="积分渠道" align="center" prop="type">
        <template #default="scope">
          <span>{{scope.row.type == 1 ? "签到" : (scope.row.type == 2 ? "日常任务" : (scope.row.type == 3 ? "积分抽奖" : "后台增加"))}}</span>
        </template>
      </el-table-column>
      <el-table-column label="增减明细" align="center" prop="title" />
      <el-table-column label="增减类型" align="center" prop="pm">
        <template #default="scope">
         <span>{{scope.row.pm === 0 ? "消耗" : "增加"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center" prop="points">
        <template #default="scope">
          <span>{{scope.row.pm == 0 ? "-" : "+"}}{{scope.row.points}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" />
<!--      <el-table-column label="任务键值" align="center" prop="taskKey" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--          <template #default="scope">-->
<!--              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:userIntegralLog:edit']">修改</el-button>-->
<!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:userIntegralLog:remove']">删除</el-button>-->
<!--          </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改获取积分记录对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="userIntegralLogRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model.number="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="增减类型" prop="pm">
          <el-select v-model="form.pm" placeholder="请选择">
            <el-option label="增加" :value="1"></el-option>
            <el-option label="消耗" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="积分数" prop="points">
          <el-input-number v-model="form.points" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="增减明细说明" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
<!--        <el-form-item label="任务键值" prop="taskKey">-->
<!--          <el-input v-model="form.taskKey" placeholder="请输入任务键值" />-->
<!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="UserIntegralLog">
import { listUserIntegralLog, getUserIntegralLog, delUserIntegralLog, addUserIntegralLog, updateUserIntegralLog } from "@/api/business/userIntegralLog";
import {
  getUser
} from "@/api/system/user";
const { proxy } = getCurrentInstance();

const userIntegralLogList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const validateUser = (rule, value, callback) => {
  getUser(value).then((response) => {
    if(response.data.user == null){
      callback(new Error('识别状态：该用户不存在'));
    }else {
      callback();
    }
  });
};
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: undefined,
    nickName: undefined,
    type: undefined,
    pm: undefined,
    points: undefined,
    title: undefined,
    taskKey: undefined
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户id不能为空", trigger: "blur" },
      {
        validator: validateUser,
        trigger: "blur",
      },
    ],
    nickName: [
      { required: true, message: "昵称不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "获取类型，1 = 签到，2 = 日常任务不能为空", trigger: "change" }
    ],
    pm: [
      { required: true, message: "0 = 支出 1 = 获得不能为空", trigger: "blur" }
    ],
    points: [
      { required: true, message: "积分数不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    taskKey: [
      { required: true, message: "任务键值不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);



/** 查询获取积分记录列表 */
function getList() {
  loading.value = true;
  listUserIntegralLog(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    userIntegralLogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    nickName: null,
    type: 10,
    pm: null,
    points: 0,
    title: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    taskKey: null
  };
  proxy.resetForm("userIntegralLogRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加积分记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getUserIntegralLog(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改获取积分记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["userIntegralLogRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateUserIntegralLog(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addUserIntegralLog(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除获取积分记录编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delUserIntegralLog(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/userIntegralLog/export', {
    ...queryParams.value
  }, `userIntegralLog_${new Date().getTime()}.xlsx`)
}

getList();
</script>
